<template>
  <div class="home">
    <div class="header">
      <div>
        <span>总支出：￥{{$store.getters.getPrice.sub}}</span>
        <span>总收入：￥{{$store.getters.getPrice.add}}</span>
      </div>
      <div class="header-type">
        <span @click="showFilter = true"
          >{{ headerTitle }} <van-icon name="arrow-down"
        /></span>
        <span @click="timeShow = true">{{currentDateFil.getFullYear()}}-{{currentDateFil.getMonth()+1}}<van-icon name="arrow-down" /></span>
      </div>
    </div>
    <!-- 筛选的时间弹出框 -->
    <van-popup v-model:show="timeShow" position="bottom" :style="{ height: '50%' }">
        <van-datetime-picker
            v-model="currentDateFil"
            @confirm="saveTime"
            type="date"
            title="选择年月日"
            :min-date="minDate"
            :max-date="maxDate"
            />

    </van-popup>

    <!-- 添加内容渲染 -->
    <div>
        <list v-for="item,index in $store.state.list" :key="index" :item="item" :type="headerTitle"></list>
    </div>
    <!-- <div class="con" v-for="item,index in $store.state.list" :key="index">
        <div class="con-top">
          <span>{{item.time.getFullYear()}}-{{item.time.getMonth()+1}}-{{item.time.getDate()}}</span>
           <div>
               <span>-￥{{item.sub}}</span>
                <span>+￥{{item.add}}</span>
           </div>
        </div>
        <ul>
            <li v-for="child,i in item.children" :key="i">
                <div><span>{{child.title}}</span>
                <span v-if="child.type=='pay'">{{child.value}}</span>
                <span v-else>{{child.value}}</span></div>
                <p>{{child.date.getMonth()+1}}-{{child.date.getDate()}}</p>
            </li>
        </ul>

    </div> -->
    
    <div class="icon" @click="show = true">
      <van-icon name="orders-o" />
    </div>
    <!-- 添加的弹出框 -->
    <van-popup
      v-model:show="show"
      position="bottom"
      :style="{ height: '75%' }"
    >
      <div class="pop-top">
        <span @click="type = 'pay'" :class="{ orange: type == 'pay' }"
          >支出</span
        >
        <span @click="type = 'income'" :class="{ orange: type == 'income' }"
          >收入</span
        >
        <span @click="shows = true"
          >{{ currentDate.getMonth() + 1 }}-{{ currentDate.getDate() }}</span
        >
        <!-- 日期弹出框 -->
        <van-popup
          v-model:show="shows"
          position="bottom"
          :style="{ height: '50%', background: '#f5f5f5' }"
        >
          <van-datetime-picker
            @confirm="time"
            v-model="currentDate"
            type="date"
            title="选择年月日"
            :min-date="minDate"
            :max-date="maxDate"
          />
        </van-popup>
      </div>
      <!-- 输入框 -->
      <div class="pop-input">￥<input type="text" v-model="value" /></div>
      <ul class="list-item">
        <li
          v-for="(item, index) in list"
          :key="index"
          v-show="item.type == type"
          @click="change(item)"
          :class="{ orange: item.title == title }"
        >
          {{ item.title }}
        </li>
      </ul>
      <!-- 备注 -->
      <van-cell-group inset>
        <van-field
          v-model="message"
          rows="2"
          autosize
          label="留言"
          type="textarea"
          maxlength="50"
          placeholder="请输入留言"
          show-word-limit
        />
      </van-cell-group>
      <!-- 键盘 -->
      <van-number-keyboard
        v-model="value"
        :hide-on-click-outside="false"
        :show="show"
        theme="custom"
        extra-key="."
        close-button-text="完成"
        @blur="add"
      />
    </van-popup>
    <!-- 筛选的弹出框 -->
    <van-popup v-model:show="showFilter" position="bottom" :style="{ height: '50%' }">
      <h4>请选择类型</h4>
      <p @click="filterClick('全部类型')" :class="'全部类型'== filTitle?'active':''">全部类型</p>
      <div class="pay">
        <p>支出</p>
        <ul>
          <li
            v-for="(item, index) in list"
            :key="index"
            v-show="item.type == 'pay'"
            :class="item.title == filTitle?'active':''"
            @click="filterClick(item.title,item)"
            
          >
            {{ item.title }}
          </li>
        </ul>
      </div>
      <div class="income">
        <p>收入</p>
        <ul>
          <li
            v-for="(item, index) in list"
            :key="index"
            v-show="item.type == 'income'"
            @click="filterClick(item.title,item)"
            :class="item.title == filTitle?'active':''"
           
          >
            {{ item.title }}
          </li>
        </ul>
      </div>
    </van-popup>
  </div>
</template>

<script>
// @ is an alias to /src
import list from '@/components/list.vue'
import { ref, reactive, toRefs } from "vue";
import { useStore } from "vuex";
export default {
    components:{
        list
    },
  setup(props) {
    const { commit,state } = useStore();
    const data = reactive({
      show: false, //添加弹出框的状态
      list: [
        { type: "pay", title: "餐饮" },
        { type: "pay", title: "交通" },
        { type: "pay", title: "购物" },
        { type: "pay", title: "娱乐" },
        { type: "pay", title: "其他" },
        { type: "income", title: "工资" },
        { type: "income", title: "奖金" },
        { type: "income", title: "转账" },
        { type: "income", title: "理财" },
        { type: "income", title: "其他" },
      ],
      type: "pay", //用来保存支出或者收入的状态
      title: "", //用来保存你点击的标题
      value: "", //输入框的值
      message: "", //留言
      shows: false, //日期弹出框的状态
      showFilter: false, //筛选弹出框的状态
      headerTitle: "全部类型",
      filTitle:'',//保存筛选的标题
      timeShow:false,
      choose:''
      
    });
    const change = (item) => {
      data.title = item.title;
    };
    // 添加
    const add = () => {
      console.log(data.type);
      commit("add", {
        type: data.type,
        title: data.title,
        value: data.value,
        date: currentDate.value,
      });
      data.show = false;
    };

    // 日期默认值
    const currentDate = ref(new Date(2021, 0, 17));
    const currentDateFil = ref(new Date(2022, 0, 17));
    const time = (val) => {
      currentDate.value = val;
      console.dir(currentDate.value);
      data.shows = false;
    };
    // 筛选
    const filterClick = (title,item) => {
     
      data.headerTitle = title;
      data.filTitle = title
      data.choose = item
     
      commit("filterClick", title);
       data.showFilter = false
    };
    // 点击确定做时间筛选
    const saveTime = (val)=>{
        console.log(val)
        currentDateFil.value = val
        data.timeShow = false
        // 时间戳筛选
        state.list = state.copyList.filter(item=>item.time.getTime() == val.getTime())



    }
    return {
      ...toRefs(data),
      change,
      add,
      currentDate,
      minDate: new Date(2020, 0, 1),
      maxDate: new Date(2025, 10, 1),
      time,
      filterClick,
      currentDateFil,
      saveTime
    };
  },
};
</script>
<style lang="scss" scoped>
.header {
  background: #327ff2;
  color: #fff;
  padding: 20px;
  .header-type {
    display: flex;
    justify-content: flex-end; //从末尾开始排
    margin-top: 10px;
  }
}
.icon {
  position: fixed;
  right: 30px;
  bottom: 100px;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  line-height: 40px;
  text-align: center;
  background: #fff;
  box-shadow: 0 0 5px 5px #eee;
  .van-icon {
    font-size: 18px;
    color: #327ff2;
  }
}
.pop-top {
  padding: 10px;
  span {
    display: inline-block;
    width: 50px;
    height: 20px;
    border-radius: 10px;
    border: 1px solid #ccc;
    line-height: 20px;
    padding: 2px 5px;
    text-align: center;
    margin: 0 15px;
  }
}
.list-item {
  display: flex;
  li {
    width: 30px;
    height: 20px;
    border-radius: 10px;
    border: 1px solid #ccc;
    line-height: 20px;
    padding: 2px 5px;
    text-align: center;
    margin: 0 10px;
    font-size: 12px;
  }
}
.orange {
  background: orange !important;
  color: #ffff;
}
.pop-input {
  padding: 10px 16px;
  width: 100%;
  height: 50px;
  font-size: 18px;
  input {
    border: none;
    width: 80%;
    height: 80%;
    border-bottom: 1px solid #ccc;
  }
}
.pay,
.income {
  ul {
    display: flex;
    li {
      width: 60px;
      height: 30px;
      border-radius: 10px;
      border: 1px solid #ccc;
      line-height: 30px;
      padding: 2px 5px;
      text-align: center;
      margin: 0 10px;
      font-size: 12px;
    }
  }
}
.active{
    background: #327ff2;
    color:#fff;
}


</style>
